﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameEngine.EditorObject;

using AimGameLibrary;
using AimGameLibrary.GameObject;

namespace AimGameEngine.EditorObject
{
    public class LinkLine : Image2D
    {
        public Connecter StartConnecter;
        public Connecter EndConnecter;

        public Vector2 Start
        {
            set
            {
                MyViewData.Position = value;
                UpdateScale();
                UpdateRotation();
            }
            get { return MyViewData.Position; }
        }

        private Vector2 end;
        public Vector2 End
        {
            set
            {
                end = value;
                UpdateScale();
                UpdateRotation();
            }
            get { return end; }
        }

        public Vector2 Length
        {
            set { }
            get { return End - Start; }
        }

        public float SetupDepth
        {
            get { return MyViewData.Depth; }
            set { MyViewData.Depth = value - 0.01f; }
        }
        public bool IsClisk;
        public bool Isconnect;
        public Syco FirstConnect;

        List<IDraw> ListDraw;
        Dictionary<String, LinkLine> MyList;

        public LinkLine(SpriteBatch inputspritebatch, Camera inputcamera, List<IDraw> ListDraw, Dictionary<String, LinkLine> MyList, Connecter StartConnecter)
            : base(inputspritebatch, inputcamera)
        {
            //SetupDepth = MyViewData.Depth;
            MyViewData.Source = new Rectangle(0, 0, 1, 1);
            IsClisk = true;
            Isconnect = false;
            this.ListDraw = ListDraw;
            this.MyList = MyList;
            this.StartConnecter = StartConnecter;
            this.FirstConnect = StartConnecter.MySyco;

            this.Start = StartConnecter.PositionToCenter;
            this.End = StartConnecter.PositionToCenter;
        }

        public override void Draw()
        {
            base.Draw();

            if (StartConnecter != null)
                MyViewData.Position = StartConnecter.PositionToCenter;

            if (EndConnecter != null)
                End = EndConnecter.PositionToCenter;

            // None Complete Connect
            if (EndConnecter == null && !Isconnect && !IsClisk)
                Remove();

            // Conmplete Connect and Delete Node
            if (EndConnecter != null && !Isconnect && !IsClisk)
                Remove();
        }

        private void UpdateScale()
        {
            MyViewData.Scale.X = Length.Length();
        }

        private void UpdateRotation()
        {
            // ArcTan X/Y
            MyViewData.Rotation = (float)Math.Atan2(Length.Y, Length.X);
        }

        public void AddMetoList()
        {
            this.ListDraw.Add(this);

            // if Name have in Dictionary
            while (MyList.ContainsKey(MyViewData.Name))
                MyViewData.Name += MyList.Count;

            if (!MyList.ContainsKey(MyViewData.Name))
                this.MyList.Add(MyViewData.Name, this);
        }
        public void Remove()
        {
            ListDraw.Remove(this);
            MyList.Remove(MyViewData.Name);

            if (StartConnecter != null && EndConnecter != null)
            {
                if (StartConnecter.MySyco == Syco.Input)
                {
                    StartConnecter.MyNode.ClearInput(StartConnecter, EndConnecter, EndConnecter.MyNode.MyData.MyName);
                    StartConnecter.MyNode.LinklineInputForSave.Remove(this);
                }
                else if (EndConnecter.MySyco == Syco.Input)
                {
                    EndConnecter.MyNode.ClearInput(EndConnecter, StartConnecter, StartConnecter.MyNode.MyData.MyName);
                    EndConnecter.MyNode.LinklineInputForSave.Remove(this);
                }
            }

            if (StartConnecter != null)
            {
                if (StartConnecter.MyNode.LinklinetoConnecter.Contains(this))
                    StartConnecter.MyNode.LinklinetoConnecter.Remove(this);

            }
            if (EndConnecter != null)
            {
                if (EndConnecter.MyNode.LinklinetoConnecter.Contains(this))
                    EndConnecter.MyNode.LinklinetoConnecter.Remove(this);

            }


            StartConnecter = null;
            EndConnecter = null;
        }

        public bool IntersecMouse(Vector2 inputPositionMouse, int inputSize)
        {
            // Cradit Rriki

            int Size = inputSize;
            Vector2 TempMouse = inputPositionMouse;
            Vector2 TempStart = this.camera.ApplyPosition(Start);
            Vector2 TempEnd = this.camera.ApplyPosition(End);
            if (Math.Abs(TempStart.X - End.X) > Math.Abs(TempStart.Y - End.Y))
            {
                float Tx = (TempMouse.X - TempStart.X) / (TempEnd.X - TempStart.X);
                float RequstMouseY = TempStart.Y + Tx * (TempEnd.Y - TempStart.Y);
                //LineRec = new Rectangle((int)(inputPositionMouse.X - (SizeLine/2)), (int)(RequstMouseY - (SizeLine/2)), SizeLine, SizeLine);
                if (Tx >= 0 && Tx <= 1 && TempMouse.Y >= RequstMouseY - Size && TempMouse.Y <= RequstMouseY + Size && TempMouse.Y >= RequstMouseY - Size)
                {
                    //DicLinkLine[K].Colors = Color.Red;
                    return true;
                }
                else
                {
                    //DicLinkLine[K].Colors = Color.White;
                    return false;
                }
            }
            else
            {
                float Ty = (TempMouse.Y - TempStart.Y) / (TempEnd.Y - TempStart.Y);
                float RequstMouseX = TempStart.X + Ty * (TempEnd.X - TempStart.X);
                //LineRec = new Rectangle((int)(RequstMouseX - (SizeLine / 2)), (int)(inputPositionMouse.Y - (SizeLine / 2)), SizeLine, SizeLine);
                if (Ty >= 0 && Ty <= 1 && TempMouse.X >= RequstMouseX - Size && TempMouse.X <= RequstMouseX + Size && TempMouse.X >= RequstMouseX - Size)
                {
                    //DicLinkLine[K].Colors = Color.Red;
                    return true;
                }
                else
                {
                    //DicLinkLine[K].Colors = Color.White;
                    return false;
                }
            }
        }
    }
}
